home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / A / AxoCalculator Package / AxoCalculator Documentation / Programming in Pascal / Example Functions next >
Encoding:
Text File  |  1993-02-02  |  2.7 KB  |  99 lines  |  [TEXT/AxoC]

  1. LocalLanguage Pascal
  2. { -------------------------------------------------
  3.   The following functions demonstrate various aspects of 
  4.   AxoCalculator's Pascal programming language. 
  5.  
  6. •  To load all functions, choose "Select All" 
  7.     under the "Edit" menu, then press "enter". 
  8.  
  9. •  To run a function, type its name followed
  10.     by any parameters, then press "enter".
  11.  
  12. •  After the functions are loaded, this file does 
  13.     not need to remain open in order to use them.
  14.  
  15. •  Here are some examples of how to use the functions.
  16.  
  17. BoxVolume (10,15,20)
  18. Factorial (10)
  19. RecursiveFact (10)
  20. SameBirthDateProb(25)
  21.  
  22. --------------------------------------------------}
  23.  
  24.  
  25. { ---------------- RectArea -------------------
  26.   This function calculates the area of a rectangle
  27.   given its height and width.
  28. -------------------------------------------------}
  29. function RectArea (height, width)
  30. begin
  31.     RectArea = height * width
  32. end
  33.  
  34. { ---------------- BoxVolume ----------------------
  35.   This function calculates the volume of a box 
  36.   given its height, width and depth. It calls the
  37.   function RectArea to get the area of the bottom of
  38.   the box, then multiplies the result by the box depth. 
  39. -------------------------------------------------}
  40. function BoxVolume (height, width, depth)
  41. begin
  42.     BoxVolume = RectArea (height, width) * depth
  43. end
  44.  
  45. { ---------------- Factorial -------------------
  46.   This function calculates the factorial of a number.
  47. -----------------------------------------------}
  48. function Factorial (number)
  49. Var
  50.     f : Real
  51.     i : Integer
  52. begin
  53.     f = 1
  54.     For i = 1 to Number do f = f * i
  55.     Factorial = f
  56. end
  57.  
  58.  
  59. { ---------------- Factorial -------------------
  60.   This function also calculates the factorial of a number,
  61.   but it uses a recursive algorithm. This approach is
  62.   inefficient, but demonstrates recursion.
  63. -----------------------------------------------}
  64. function RecursiveFact (number)
  65. begin
  66.     if number > 1 then
  67.         RecursiveFact = number * RecursiveFact (number - 1)
  68.     else
  69.         RecursiveFact = 1
  70. end
  71.  
  72.  
  73. { ---------------- SameBirthDateProb -------------------
  74.   This function calculates the probability that two or more
  75.   people in a group have the same birth date, given the
  76.   number of people in the group. It works by calculating
  77.   1 - the probability that everyone in the group has a 
  78.   different birth date.
  79. -----------------------------------------------}
  80. function SameBirthDateProb (numberOfPeople)
  81. begin
  82.     if (numberOfPeople < 2) then 
  83.         DiffBirthDateProb = 1
  84.     else 
  85.     begin
  86.         if (numberOfPeople > 365) then 
  87.             DiffBirthDateProb = 0
  88.         else
  89.         begin
  90.             DiffBirthDateProb = 1
  91.             For i = 2 to numberOfPeople do 
  92.                 DiffBirthDateProb = DiffBirthDateProb * (366-i) / 365
  93.         end
  94.     end
  95.     SameBirthDateProb = 1 - DiffBirthDateProb
  96. end
  97.  
  98.  
  99.